String compareTo
Course- Java >
The java string compareTo() method compares the given string with current string lexicographically. It returns positive number, negative number or 0.
If first string is greater than second string, it returns positive number (difference of character value). If first string is less than second string, it returns negative number and if first string is equal to second string, it returns 0.
- s1 > s2 => positive number
- s1 < s2 => negative number
- s1 == s2 => 0
Signature
- public int compareTo(String anotherString)
Parameters
anotherString: represents string that is to be compared with current string
Returns
an integer value
Java String compareTo() method example
- public class LastIndexOfExample{
- public static void main(String args[]){
- String s1="hello";
- String s2="hello";
- String s3="meklo";
- String s4="hemlo";
- System.out.println(s1.compareTo(s2));
- System.out.println(s1.compareTo(s3));
- System.out.println(s1.compareTo(s4));
- }}
Output:
0 -5 -1